Page({
  data: {
    ingredients: []
  },

  onLoad(options) {
    // 获取传递过来的食材数据
    const ingredients = JSON.parse(options.ingredients || '[]');
    this.setData({ ingredients });
  },

  // 删除食材
  deleteIngredient(e) {
    const { index } = e.currentTarget.dataset;
    const ingredients = this.data.ingredients;
    ingredients.splice(index, 1);
    this.setData({ ingredients });
  },

  // 完成调整
  onFinish() {
    // 将调整后的数据返回上一页
    const pages = getCurrentPages();
    const prevPage = pages[pages.length - 2];
    prevPage.setData({
      ingredients: this.data.ingredients
    });
    wx.navigateBack();
  },

  // 拖拽排序相关方法
  onDragStart(e) {
    this.setData({
      dragIndex: e.currentTarget.dataset.index
    });
  },

  onDragMove(e) {
    const { dragIndex } = this.data;
    const targetIndex = e.currentTarget.dataset.index;
    
    if (dragIndex === targetIndex) return;
    
    const ingredients = this.data.ingredients;
    const temp = ingredients[dragIndex];
    ingredients[dragIndex] = ingredients[targetIndex];
    ingredients[targetIndex] = temp;
    
    this.setData({
      ingredients,
      dragIndex: targetIndex
    });
  },

  // 添加返回方法
  navigateBack() {
    wx.navigateBack();
  }
}); 